﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace NCT.MRS.DataAccessLayer.Repositories
{
    public abstract class AbstractRepository<T> : IRepository<T> where T : class
    {
        public AbstractRepository(IAppContext objectContext)
        {
            _objects = objectContext.Set<T>();
            _context = objectContext;
        }

        protected IDbSet<T> EntityObject
        {
            get
            {
                return _objects;
            }
        }

        protected IAppContext Context
        {
            get
            {
                return _context;
            }
        }

        private readonly IDbSet<T> _objects;
        private readonly IAppContext _context;

        public IEnumerable<T> GetAll()
        {
            return _objects.AsQueryable();
        }

        public IEnumerable<T> GetByQuery(Expression<Func<T, bool>> where)
        {
            return _objects.Where(where);
        }

        public void Delete(T entity)
        {
            _objects.Remove(entity);
        }

        public void Add(T entity)
        {
            _objects.Add(entity);
        }

        public void Update(T entity)
        {
            _objects.Attach(entity);
            _context.Entry(entity).State = System.Data.EntityState.Modified;
        }

        abstract public T GetById(int Id);

    }
}
